package org.game.gobang.client.constant;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import org.game.constant.APIConstant;
import org.game.gobang.client.GobangClient;
import org.game.gobang.client.support.GobangButton;
import org.game.gobang.client.support.SuccessBox;
import org.game.result.RestResult;
import org.game.result.RestResultBuilder;
import org.game.result.ResultCode;
import org.game.util.JsonUtil;

import java.util.ArrayList;
import java.util.List;

public class ClientConstant {
    public static Channel channel;
    public static ChannelHandlerContext ctx;
    public static final ObservableList rooms =
            FXCollections.observableArrayList();
    public static Stage roomStage;
    public static String clickedColor = "#b47eae";
    public static Label name;
    public static Label playerLabel;
    public static String player;
    public static GridPane map;
    public static Boolean canClick = true;
    public static List<GobangButton> gobangButtonList = new ArrayList<>();

    public static void refreshMap() {
        int width = (int) APIConstant.CONFIG.get("width");
        int height = (int) APIConstant.CONFIG.get("height");
        int cellWidth = (int) APIConstant.CONFIG.get("cellWidth");
        int cellHeight = (int) APIConstant.CONFIG.get("cellHeight");

        clearMap();

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                GobangButton gobangButton = new GobangButton(cellWidth, cellHeight, i * height + j);
                gobangButtonList.add(gobangButton);
                ClientConstant.map.add(gobangButton, j, i);
            }
        }
    }

    public static void clearMap() {
        gobangButtonList = new ArrayList<>();
    }

    public static void win() {
        int width = (int) APIConstant.CONFIG.get("width");
        int height = (int) APIConstant.CONFIG.get("height");

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (!gobangButtonList.get(i * height + j).getColor().equals("#f9d3e3") && win(i, j, height)) {
                    new SuccessBox(gobangButtonList.get(i * height + j).getColor());

                    RestResult restResult = new RestResultBuilder().success(ResultCode.WIN);
                    restResult.setData((Object) gobangButtonList.get(i * height + j).getColor());
                    ClientConstant.ctx.writeAndFlush(JsonUtil.toJson(restResult));

                    return;
                }
            }
        }
    }

    private static boolean win(int x, int y, int height) {
        int[][] pos = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 0}, {-1, -1}, {1, -1}, {-1, 1}};

        for (int i = 0; i < pos.length; i++) {
            int number = 0;

            int col = x;
            int row = y;

            while (true) {
                if (!gobangButtonList.get(col * height + row).getColor()
                        .equals(gobangButtonList.get(
                                (col + pos[i][0]) * height + (row + pos[i][1])
                        ).getColor())) {
                    break;
                }

                col = col + pos[i][0];
                row = row + pos[i][1];

                number++;

                if (number == 4) {
                    return true;
                }
            }
        }

        return false;
    }
}
